<%@page import="bll.EnumName"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.*,util.*,dal.*,model.*, bll.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add New Purchase</title>
<link rel="stylesheet" href="css/common.css" />
<link rel="stylesheet" href="css/inputForm.css" />
<link rel="stylesheet" href="css/listForm.css" />
<link rel="stylesheet" href="css/jquery-ui-1.8.14.custom.css" type="text/css" />
<link rel="stylesheet" href="css/hyjack.css" type="text/css" />
<link rel="stylesheet" href="css/datepicker.css" type="text/css" />
<link rel="stylesheet" href="css/themes/ui-lightness/jquery.ui.all.css" />
<link rel="stylesheet" href="css/jquery-ui-timepicker.css" type="text/css" />
<link rel="stylesheet" href="css/jquery-datepicker.css" />
<link rel="stylesheet" href="css/jquery-dialog.css" />

<script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.select.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.core.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.widget.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.timepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.js"></script>
<script language="javascript" type="text/javascript" src="js/ajaxQuickInsert.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.numeric.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.tools.min.js"></script>
<script language="javascript" type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>


<script language="javascript" type="text/javascript">
$(document).ready(pageLoaded);

function pageLoaded()
{
	turnOnNumbericInput();
	initTooltip();	
	initDatePicker();
	initDateTimePicker();

} 

var requiredInputs = [['name','name_required']];
var intInputs = [];
var doubleInputs = [];
var dateInputs = [['payment_date_date', 'payment_date_date_invalid']];
var timeInputs = [['order_time_date','order_time_date_invalid'],['plan_delivery_time_date','plan_delivery_time_date_invalid'],['real_delivery_time_date','real_delivery_time_date_invalid']];

function checkInputs() {
	var valid = true;

	if (checkRequiredInputs(requiredInputs) == false) {
		valid = false;
	}
	if (checkIntInputs(intInputs) == false) {
		valid = false;
	}
	if (checkDoubleInputs(doubleInputs) == false) {
		valid = false;
	}
	if (checkDateInputsForCorrectedFormatOnly(dateInputs, dateFormat) == false) {
		valid = false;
	}	
	if (checkTimeInputsForCorrectFormatOnly(timeInputs) == false) {
		valid = false;
	}

	// SPECIFIC VALIDATE CODE HERE		

	return valid;
}
function serializeProject()
{
	getCascadeDropdowList('#id_project','#id_event','event');
	getCascadeDropdowList('#id_project','#id_checklist','checklist');
	getCascadeDropdowList('#id_project','#id_quotation','quotation');
	serializeChecklist();
	serializeQuotation();
	
}
function serializeQuotation()
{
	getCascadeDropdowList('#id_quotation','#id_quotation_item','quotationItem');
}
function serializeChecklist()
{
	getCascadeDropdowList('#id_checklist','#id_task','checklistItem');
}
	
</script>
</head>
<body>
	<div class="divContainer">
		<%@ include file="menu.jsp"%>
				<form id="inputForm" action="purchase?mode=insert" method="post" onSubmit="return checkInputs()">
					<table class="tblInput">
						<tr>
							<td class="inputHeader">ADD NEW PURCHASE</td>
						</tr>
						<tr>
					<td style="padding-left: 200px; padding-right: 200px;">
						<table cellspacing="0px">
						<tr class="trField">
							<td class="tdFieldName">Name</td>
							<td class="tdFieldInput">
								<input class="inputText" type="text" id="name" name="name" maxlength="255" title="Name of new purchase." onblur="validateRequiredInput('name');" />
							</td>
							<td class="tdErrMsg">
								<span id="name_required" class="errMsg" style="display: none;" title="Name is required">(!)</span> 
							</td>
							
							<td rowspan="10"></td>
							
							<td class="tdFieldName">Payment Date</td>
							<td class="tdFieldInput">
								<input type="text" maxlength="10" class="inputDate" id="payment_date_date" name="payment_date_date" 
									title="Click to choose payment date." onchange="validateDateInput('payment_date_date',false)" />
							</td>
							<td class="tdErrMsg">
								<span id="payment_date_date_invalid" class="errMsg" style="display: none;" title="You've entered an invalid date.<br/>The format is <b>dd/mm/yyyy at hh:mm</b>">(!)</span>
							</td>
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Code</td>
							<td class="tdFieldInput">
								<input class="inputText" type="text" id="code" name="code" maxlength="20" title="Code of new purchase." />
							</td>
							<td class="tdErrMsg">
							</td>
							<td class="tdFieldName">Order Time</td>
							<td class="tdFieldInput"><input type="text" maxlength="19" class="inputDateTime" id="order_time_date" name="order_time_date" title="Click to choose order time.<br/>Note: the format is <b>dd/mm/yyyy at hh:mm</b>."  onchange="validateDateTimeInput('order_time_date', false);" /> 
							</td>
							<td class="tdErrMsg">
								<span id="order_time_date_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.<br/>The format is <b>dd/mm/yyyy at hh:mm</b>">(!)</span>
							</td>
							
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Description</td>
							<td class="tdFieldInput">
								<textarea id="description" name="description" rows="5" class="inputTextarea" title="Description for new purchase."></textarea>
							</td>
							<td class="tdErrMsg">
							</td>
							<td class="tdFieldName">Plan Delivery Time</td>
							<td class="tdFieldInput"><input type="text" maxlength="19" class="inputDateTime" id="plan_delivery_time_date" name="plan_delivery_time_date" title="Click to choose plan delivery time.<br/>Note: the format is <b>dd/mm/yyyy at hh:mm</b>."  onchange="validateDateTimeInput('plan_delivery_time_date', false);" /> 
							</td>
							<td class="tdErrMsg">
								<span id="plan_delivery_time_date_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.<br/>The format is <b>dd/mm/yyyy at hh:mm</b>">(!)</span>
							</td>
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Remark</td>
							<td class="tdFieldInput">
								<textarea id="remark" name="remark" rows="5" class="inputTextarea" title="remark for new purchase."></textarea>
							</td>
							<td class="tdErrMsg">
							</td>
							<td class="tdFieldName">Real Delivery Time</td>
							<td class="tdFieldInput"><input type="text" maxlength="19" class="inputDateTime" id="real_delivery_time_date" name="real_delivery_time_date" title="Click to choose real delivery time.<br/>Note: the format is <b>dd/mm/yyyy at hh:mm</b>."  onchange="validateDateTimeInput('real_delivery_time_date', false);" /> 
							</td>
							<td class="tdErrMsg">
								<span id="real_delivery_time_date_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.<br/>The format is <b>dd/mm/yyyy at hh:mm</b>">(!)</span>
							</td>
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Status</td>
							<td class="tdFieldInput"><select id="status" name="status" class="hyjack" title="Choose one status for the purchase.">
								<%=HtmlUtil.genEnumSelect(EnumName.PurchaseStatus, null) %>
								</select>
							</td>
							<td class="tdErrMsg"></td>
							<td class="tdFieldName">Project</td>
							<td class="tdFieldInput">
								<select id="id_project" name="id_project" class="hyjack" onchange="serializeProject();" >
									<%=HtmlUtil.<ProjectModel>genModelSelect(CacheUtil.getProjectList(), null, Integer.MIN_VALUE, false, true)%>
								</select>
							</td>
							<td class="tdErrMsg">
							</td>		
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Purchaser</td>
							<td class="tdFieldInput">
								<select id="purchaser" name="purchaser" class="hyjack" title="Choose a purchaser for this purchase.">
								<%=HtmlUtil.<EmployeeModel>genModelSelect(CacheUtil.getEmployeeList(), null, Integer.MIN_VALUE, false, true)%>
								</select>
								<input class="btnAdd" type="button" id="addNewPurchaser" value="" onclick="ajaxAddItem('#purchaser', 'employee', 'name', 'Please enter new employee name!');" title="Click here to add new employee."/>
							</td>
							<td class="tdErrMsg">
							</td>
							<td class="tdFieldName">Event</td>
							<td class="tdFieldInput">
								<select id="id_event" name="id_event" class="hyjack" >
									<option value="">--------------------</option>
								</select>
							</td>
							<td class="tdErrMsg">
							</td>	
							
							
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Supplier</td>
							<td class="tdFieldInput">
								<select id="supplier" name="supplier" class="hyjack" title="Choose a supplier for this purchase.">
								<%=HtmlUtil.<CompanyModel>genModelSelect(CacheUtil.getCompanyList(), null, Integer.MIN_VALUE, false, true)%>
								</select>
								<input class="btnAdd" type="button" id="addNewSupplier" value="" onclick="ajaxAddItem('#supplier', 'company', 'short_name', 'Please enter new company short name!');" title="Click here to add new company."/>
							</td>
							<td class="tdErrMsg">
							</td>
							<td class="tdFieldName">Checklist</td>
							<td class="tdFieldInput">
								<select id="id_checklist" name="id_checklist" class="hyjack" onchange="serializeChecklist();" >
									<option value="">--------------------</option>
								</select>
							</td>
							<td class="tdErrMsg">
							</td>	
							
							
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Currency</td>
							<td class="tdFieldInput"><select id="id_currency" name="id_currency" class="hyjack" title="Choose a currency for the purchase.">
								<%=HtmlUtil.<CurrencyModel>genModelSelect(CacheUtil.getCurrencyList(), null,CacheUtil.getDefaultCurrencyId()) %>
								</select>
								<input class="btnAdd" type="button" id="addNewCurrency" value="" onclick="ajaxAddItem('#id_currency', 'currency', 'name', 'Please enter currency name!');" title="Click here to add new currency." />
							</td>
							<td class="tdErrMsg"></td>
							<td class="tdFieldName">Task</td>
							<td class="tdFieldInput">
								<select id="id_task" name="id_task" class="hyjack" >
									<option value="">--------------------</option>
								</select>
							</td>
							<td class="tdErrMsg">
							</td>	
							
						</tr>
						<tr class="trField">
							<td class="tdFieldName">Additional Cost</td>
							<td class="tdFieldInput">
								<input class="inputNumeric" type="text" id="additional_cost" name="additional_cost" value="0" title="Additional cost of the purchase. <br/>Note: numeric only." maxlength="20" />
							</td>
							<td class="tdErrMsg">
							</td>
							<td class="tdFieldName">Quotation</td>
							<td class="tdFieldInput">
								<select id="id_quotation" name="id_quotation" class="hyjack" onchange="serializeQuotation();" >
									<option value="">--------------------</option>
								</select>
							</td>
							<td class="tdErrMsg">
							</td>	
								
						</tr>
						<tr>
							<td class="tdFieldName">Discount</td>
							<td class="tdFieldInput">
								<input class="inputNumeric" type="text" id="discount" name="discount" value="0" title="Purchase discount. <br/>Note: numeric only." maxlength="20" />
							</td>
							<td class="tdErrMsg">
							</td>
							
							<td class="tdFieldName">Quotation Item</td>
							<td class="tdFieldInput">
								<select id="id_quotation_item" name="id_quotation_item" class="hyjack" >
									<option value="">--------------------</option>
								</select>
							</td>
							<td class="tdErrMsg">
							</td>
						</tr>
						
						<tr class="trInputBottom">
							<td colspan="7" class="tdSubmit" style="text-align: center;"><input class="btnSubmit" type="submit" value="Submit" title="Click to save above information."/></td>

						</tr>
					</table>
				</td>
			</tr>
		</table>
	</form>
	<%
	
		String urlPurchaseList = "purchaseList.jsp?embedded=true";
	%>
		<jsp:include page='<%=urlPurchaseList%>'></jsp:include>
	</div>
</body>
</html>